package org.acghub.mtdb.xmlgen;

import org.acghub.mtdb.common.*;
import org.acghub.mtdb.xmlgen.maker.MtdbMaker;

import java.util.Collection;

/**
 * @author zhouhe
 */

public class Generator {
    private final Config config;
    private Mtdb mtdb;

    public Generator(Config config) {
        this.config = config;
        Constants.setConfig(config);
    }

    public void generate() throws Exception {
        // 0.环境参数初始化
        config.init();
        // 2.初始化类型工厂
        TypeFactory.init();
        // 3.构造mtdb
        this.mtdb = TypeFactory.create(Mtdb.class, Root.DEFAULT_ROOT, config.getElement());
        // 4.编译
        this.compile();
        // 5.make生成
        this.make();
    }

    private void compile() throws Exception {
        Collection<Compilable> list = mtdb.compile();
        if (!list.isEmpty()) {
            System.err.println("================================ 编译❌😔 ================================");
            for (Compilable compile : list) {
                System.err.println("\t" + compile);
            }
            System.exit(-1);
        }
        System.out.println("================================ 编译✅😊 ================================");
    }

    private void make() throws Exception {
        config.make();
        new MtdbMaker(mtdb, config).make();
        System.out.println("================================ make✅😊 ================================");
    }
}
